#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QFileDialog"
#include <QMessageBox>
#include "encode.h"
#include <QStringListModel>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->lineEdit_pwd->setEchoMode(QLineEdit::Password);
    ui->pngLabel->installEventFilter(this);
    QStringListModel *model;
    QString str;
    QStringList arguments = QCoreApplication::arguments();
    arguments.removeAt(0);
    model = new QStringListModel(this);
    model->setStringList(arguments);
    ui->listView->setModel(model);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    close();
}

void MainWindow::on_pushButton_encode_clicked()
{
    QString pth = ui->lineEdit_location->text();
    QStringList arguments = QCoreApplication::arguments();
    arguments.removeAt(0);

    QString fileName = ui->lineEdit_filename->text();
    pth = pth + "/" + fileName + ".ecr";
    QStringList srcList = arguments;
    QString pwd = ui->lineEdit_pwd->text();
    int alg = ui->comboBox_algorithm->currentIndex() - 1;
    int mod = ui->comboBox_mode->currentIndex() - 1;
    //QMessageBox::information(NULL, "Decode", "加密成功！",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    FileOperation fo(srcList, pth);
    switch (fo.handleFileEncryption(alg, mod, pwd)){
    case 0:
    QMessageBox::information(NULL, "Decode", "加密成功！",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    break;
    case 2:
    QMessageBox::information(NULL, "Decode", "加密失败！",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    }
    //code = new encode(this);
    //code->show();
}

void MainWindow::on_pushButton_choose_clicked()
{
    QString srcPath = QFileDialog::getExistingDirectory(this, "choose src Directory","/");
    if(srcPath.isEmpty())
    {
        return;
    }else {
        ui->lineEdit_location->setText(srcPath);}
}

void MainWindow::on_comboBox_algorithm_currentIndexChanged()
{
    switch (ui->comboBox_algorithm->currentIndex()) {
    case 0:
        ui->comboBox_mode->clear();
        break;
    case 1:
        ui->comboBox_mode->clear();
        ui->comboBox_mode->insertItem(0, " ");
        ui->comboBox_mode->insertItem(1, "ECB模式");
        ui->comboBox_mode->insertItem(2, "CBC模式");
        ui->comboBox_mode->insertItem(3, "CFB模式");
        ui->comboBox_mode->insertItem(4, "OFB模式");
        break;
    case 2:
        ui->comboBox_mode->clear();
        ui->comboBox_mode->insertItem(0, " ");
        ui->comboBox_mode->insertItem(1, "ECB模式");
        ui->comboBox_mode->insertItem(2, "CBC模式");
        ui->comboBox_mode->insertItem(3, "CFB模式");
        ui->comboBox_mode->insertItem(4, "OFB模式");
        break;
    case 3:
        ui->comboBox_mode->clear();
        ui->comboBox_mode->insertItem(0, " ");
        ui->comboBox_mode->insertItem(1, "ECB模式");
        ui->comboBox_mode->insertItem(2, "CBC模式");
        ui->comboBox_mode->insertItem(3, "CFB模式");
        ui->comboBox_mode->insertItem(4, "OFB模式");
        break;
    /*case 5:
        ui->comboBox_mode->clear();
        ui->comboBox_mode->insertItem(0, " ");
        ui->comboBox_mode->insertItem(1, "SHA1模式");
        ui->comboBox_mode->insertItem(2, "SHA224模式");
        ui->comboBox_mode->insertItem(3, "SHA384模式");
        ui->comboBox_mode->insertItem(4, "SHA512模式");
        ui->comboBox_mode->insertItem(5, "RIPEMD160模式");
        ui->comboBox_mode->insertItem(6, "MD5模式");*/
    }
}

bool MainWindow::eventFilter(QObject *watched, QEvent *event){
    switch (event->type()) {
    case QEvent::Enter:
        ui->lineEdit_pwd->setEchoMode(QLineEdit::Normal);
        break;
    case QEvent::Leave:
        ui->lineEdit_pwd->setEchoMode(QLineEdit::Password);
        break;
    default:
        break;
    }
    return QWidget::eventFilter(watched,event);
}

QString MainWindow::text() const
{
    return ui->lineEdit_pwd->text();
}
